/**
 * Created by shao-yikun on 2017/4/30.
 */

$(function () {
    gMainDT = $("#mainDataTables").DataTable({
        ajax: {
            url: "../rds_logController/getLog4DT",
            type: "POST",
            data: function (d) {
                d.search = $('#mainDataTables').DataTable().search(this.value);
            }
        },
        columns: [{
            data: null,
            targets: 0
        }, {
            data: "userName"
        }, {
            data: "orgName"
        }, {
            data: null,
            render: function (data, type, row) {
                var str = "正常";
                if(row.orgState + row.userState != 0) {
                    str = "停用";
                }
                return str;
            }
        }, {
            data: "ip"
        }, {
            data: "userLoginTime"
        }],
        dom: "<'row'<'col-md-4'l><'#mainToolbar.col-md-8'>r>t<'row'<'col-md-6'i><'col-md-6'p>>",
        initComplete: function () {
            // 下拉工具栏按钮的权限
            $("#mainToolbar").empty();
            var strToolBar = "<div class='pull-right'><div class='btn-group'>" +
                "<button type='button' class='btn btn-primary btn-sm btn-flat'><i class='fa fa-cog'></i> 操作</button>" +
                "<button type='button' class='btn btn-primary btn-sm btn-flat dropdown-toggle' " +
                "data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>" +
                "<span class='caret'></span><span class='sr-only'>切换下拉</span></button>" +
                "<ul class='dropdown-menu'>" +
                "<li>" +
                "<button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='gMainDT.ajax.reload(null, false);'><i class='fa fa-refresh'></i> 刷新</button>" +
                "</li>";

            strToolBar += "</ul></div>" +
                "<div class='input-group input-group-sm'>" +
                "<input type='text' class='form-control' id='searchText' placeholder='用户名/公司名'>" +
                "<span class='input-group-btn'>" +
                "<button class='btn btn-primary btn-flat' type='button' onclick='$(this).searchDT()'>" +
                "<i class='fa fa-search'></i></button></span></div></div>";
            $("#mainToolbar").append(strToolBar);
        },
        processing: true,
        serverSide: true,
        ordering: false,
        select: false,
        autoFill: true,
        displayLength: 20,
        lengthMenu: [[5, 10, 15, 20, 50, 100, 150, -1], [5, 10, 15, 20, 50, 100, 150, "All"]],
        language: {
            url: "zh_cn.json"
        },
        fnDrawCallback: function () {// 序号自增回调函数
            var api = this.api();
            var startIndex = api.context[0]._iDisplayStart;
            api.column(0).nodes().each(function (cell, i) {
                cell.innerHTML = startIndex + i + 1;
            });
        }
    });
});

$.fn.searchDT = function () {
    gMainDT.search($("#searchText").val()).draw();
};